package com.zhuangjie.mvc.ws.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ContextPathHelper {
    @Value("${server.servlet.context-path}")
    private String contextPath;



    /**
     * 完整请求根路径
     *
     * @param explicitPath 显式路径
     * @return {@link String}
     */
    public String fullRequestRootPath(String explicitPath) {
        return (contextPath + "/" + explicitPath).replaceAll("//+", "/");
    }

    /**
     * 删除上下文路径
     *
     * @param requestPath 请求路径
     * @return {@link String}
     */
    public String removeContextPath(String requestPath) {
        if (requestPath.startsWith(contextPath)) {
            return requestPath.substring(contextPath.length());
        }
        return requestPath;
    }
}
